﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace AAA.Base.Util
{
    public class IOHelper
    {
        public static List<string> SplitFile(string strSourceFile, string strTargetFile, int iLinesPerFile)
        {
            bool hasData = true;
            string strLine;
            StreamReader sr = null;
            StreamWriter sw = null;
            string strFilename;
            List<string> lstFileList = new List<string>();
            try
            {
                sr = new StreamReader(strSourceFile, Encoding.Default);                

                while (hasData)
                {
                    strFilename = strTargetFile + lstFileList.Count.ToString("000") + ".txt";
                    sw = new StreamWriter(strFilename, false, Encoding.Default);
                    for (int i = 0; i < iLinesPerFile; i++)
                    {
                        strLine = sr.ReadLine();

                        if (strLine == null)
                        {
                            hasData = false;
                            break; ;
                        }

                        sw.WriteLine(strLine);
                    }
                    sw.Close();
                    lstFileList.Add(strFilename);
                }

            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                if (sr != null)
                    sr.Close();

                if (sw != null)
                    sw.Close();
            }
            return lstFileList;
        }

        public static string[] GetFileList(string strDirectory)
        {
            List<string> lstFileList = new List<string>();



            return lstFileList.ToArray<string>();
        }

        public static string CreateDirectory(string strDirectory)
        {
            try
            {
                if (Directory.Exists(strDirectory))
                    return null;

                Directory.CreateDirectory(strDirectory);
            }
            catch (Exception ex)
            {
                return ex.Message + "," + ex.StackTrace;
            }
            
            return null;
        }
       
        public static string ConvertDirectory(string strDirectory)
        {
            return ConvertDirectory(Environment.CurrentDirectory, strDirectory);
        }

        public static string ConvertDirectory(string strBaseDirectory, string strDirectory)
        {
            if (strDirectory.StartsWith(".") == false)
                return strDirectory;

            return strBaseDirectory.EndsWith("\\")
                    ? strBaseDirectory + strDirectory.Substring(2)
                    : strBaseDirectory + "\\" + strDirectory.Substring(2);
        }

        public static string ExecuteCommand(string strExecuteFile, string strWorkingDirectory, string strParam)
        {
            try
            {
                //'/s' : Specifies regsvr32 to run silently and to not display any message boxes.
                string strFileInfo = strParam;
                Process reg = new Process();
                //This file registers .dll files as command components in the registry.
                reg.StartInfo.FileName = strExecuteFile;
                reg.StartInfo.Arguments = strFileInfo;
                reg.StartInfo.UseShellExecute = false;
                reg.StartInfo.CreateNoWindow = true;
                reg.StartInfo.RedirectStandardOutput = true;
                if (strWorkingDirectory != null)
                    reg.StartInfo.WorkingDirectory = strWorkingDirectory;
                reg.Start();
                //reg.WaitForExit();
                try
                {
                    reg.CloseMainWindow();
                }
                catch { }
                reg.Close();
            }
            catch (Exception ex)
            {
                return ex.Message + "\n" + ex.StackTrace;
            }
            return null;
        }

        private static string DllRegister(string strWorkingDirectory, string strFilename, string strParam)
        {
            try
            {
                //'/s' : Specifies regsvr32 to run silently and to not display any message boxes.
                string strFileInfo = strParam + " " + "\"" + strFilename + "\"";
                Process reg = new Process();
                //This file registers .dll files as command components in the registry.
                reg.StartInfo.FileName = "regsvr32.exe";
                reg.StartInfo.Arguments = strFileInfo;
                reg.StartInfo.UseShellExecute = false;
                reg.StartInfo.CreateNoWindow = true;
                reg.StartInfo.RedirectStandardOutput = true;
                if (strWorkingDirectory != null)
                    reg.StartInfo.WorkingDirectory = strWorkingDirectory;
                reg.Start();
                reg.WaitForExit();
                reg.Close();
            }
            catch (Exception ex)
            {
                return ex.Message + "\n" + ex.StackTrace;
            }
            return null;
        }

        public static string UnregisteDll(string strFilename, bool isSilently)
        {
            return UnregisteDll(null, strFilename, isSilently);
        }
        public static string UnregisteDll(string strWorkingDirectory, string strFilename, bool isSilently)
        {
            return DllRegister(strWorkingDirectory, strFilename, isSilently ? "/us" : "/u");
        }

        public static string RegisteDll(string strFilename, bool isSilently)
        {
            return RegisteDll(null, strFilename, isSilently);
        }

        public static string RegisteDll(string strWorkingDirectory, string strFilename, bool isSilently)
        {
            return DllRegister(strWorkingDirectory, strFilename, isSilently ? "/is" : "/i");
        }
    }
}
